package org.ufpe.cin.ip.projeto.Relatorio;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.ufpe.cin.ip.projeto.Dados.BaseClasses.CommonPlayer;
import org.ufpe.cin.ip.projeto.Dados.BaseClasses.Mesa;

public class GeradorRelatorio implements Iterable<IRelatorio>, Serializable{
	private static final long serialVersionUID = 1L;
	private List<IRelatorio> relatorios = new ArrayList<IRelatorio>();

	@SuppressWarnings("unchecked")
	public File getRelatorioJogadores() {
		File arq = new File("Files/Relatorios/RelatorioJogadores.txt");
		try{
			FileInputStream fi = new FileInputStream("Files/Relatorios/relatorios.ip");
			ObjectInputStream oi = new ObjectInputStream(fi);
			relatorios =(List<IRelatorio>) oi.readObject();
			oi.close();
			
			String content = "";
			for(int i = 0; i < relatorios.size(); i++){
				if(relatorios.get(i)instanceof RelatorioJogadores){
					content += ((RelatorioJogadores)relatorios.get(i)).relatorio+"\n";
				}
			}
			BufferedWriter out = new BufferedWriter(new FileWriter(arq));
			
			out.write(content);
			out.close();
			
			
		}catch(Exception e){
			e.printStackTrace();
		}                          

		return arq;
	}
	
	@SuppressWarnings("unchecked")
	public File getRelatorioMesas(){
		File arq = new File("Files/Relatorios/RelatorioMesas.txt");
		try{
			FileInputStream fi = new FileInputStream("Files/Relatorios/relatorios.ip");
			ObjectInputStream oi = new ObjectInputStream(fi);
			relatorios =(List<IRelatorio>) oi.readObject();
			oi.close();
			
			String content = "";
			for(int i = 0; i < relatorios.size(); i++){
				if(relatorios.get(i)instanceof RelatorioMesa){
					content += ((RelatorioMesa)relatorios.get(i)).content+"\n";
				}
			}
			BufferedWriter out = new BufferedWriter(new FileWriter(arq));
			
			out.write(content);
			out.close();
			
			
		}catch(Exception e){
			e.printStackTrace();
		}                          

		return arq;
	}
	
	public void gerarRelatoriosIP() throws IOException{
		try {
			FileOutputStream fo = new FileOutputStream("relatorios.ip");
			ObjectOutputStream oo = new ObjectOutputStream(fo);
			oo.writeObject(relatorios);
			oo.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void gerarRelatorioMesa(Mesa mesaPrincipal, String path) throws IOException {
		RelatorioMesa aux = new RelatorioMesa();
		aux.gerarRelatorio(mesaPrincipal, path);
		relatorios.add(aux);
	}

	public void gerarRelatorioJogadore(CommonPlayer[] jogadores, String path) throws IOException {
		RelatorioJogadores aux = new RelatorioJogadores();
		aux.gerarRelatorio(jogadores, path);
		relatorios.add(aux);
	}

	@Override
	public Iterator<IRelatorio> iterator() {
		return relatorios.iterator();
	}


}
